﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using IDAL;
using System.Data.SqlClient;
using System.Data;
using Model;
using Newtonsoft.Json;
namespace DAL
{
    public class Feeder_DAL:IFeeder
    {
        SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=MengQiqidb;Integrated Security=True");//数据库连接对象

        #region 添加供应商基本信息
        public int Add(Feeder_tb model)
        {
            SqlCommand cmd = new SqlCommand("p_addFeederData", conn);//创建命令对象
            cmd.CommandType = CommandType.StoredProcedure;//声明存储过程
            SqlParameter[] pars = { 
                                  new SqlParameter("@FeederName",model.FeederName),
                                  new SqlParameter("@Address",model.Address),
                                  new SqlParameter("@FeederRemark",model.FeederRemark),
                                  new SqlParameter("@LinkMan",model.LinkMan),
                                  new SqlParameter("@Tel",model.Tel),
                                  };//传递参数
            cmd.Parameters.AddRange(pars);//添加到命令对象
            try//异常处理机制
            {
                conn.Open();//打开数据库
                int result = cmd.ExecuteNonQuery();//返回受影响行数
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();//关闭数据库
                conn.Dispose();//释放内存
            }
        }
        #endregion

        #region 修改供应商基本信息
        public int Upt(Feeder_tb model)
        {
            SqlCommand cmd = new SqlCommand("p_uptFeederData", conn);//创建命令对象
            cmd.CommandType = CommandType.StoredProcedure;//声明存储过程
            SqlParameter[] pars = { 
                                  new SqlParameter("@FeederID",model.FeederID),
                                  };//传递参数
            cmd.Parameters.AddRange(pars);//添加到命令对象
            try//异常处理机制
            {
                conn.Open();//打开数据库
                int result = cmd.ExecuteNonQuery();//返回受影响行数
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();//关闭数据库
                conn.Dispose();//释放内存
            }
        }
        #endregion

        #region 删除供应商基本信息
        public int Del(object id)
        {
            SqlCommand cmd = new SqlCommand("p_delFeederData", conn);//创建命令对象
            cmd.CommandType = CommandType.StoredProcedure;//声明存储过程
            SqlParameter[] pars = { 
                                  new SqlParameter("@FeederID",id),
                                  };//传递参数
            cmd.Parameters.AddRange(pars);//添加到命令对象
            try//异常处理机制
            {
                conn.Open();//打开数据库
                int result = cmd.ExecuteNonQuery();//返回受影响行数
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();//关闭数据库
                conn.Dispose();//释放内存
            }
        }
        #endregion

        #region 获取供应商基本信息
        public List<Feeder_tb> GetData()
        {

            SqlCommand cmd = new SqlCommand("p_getFeederData", conn);//创建命令对象
            cmd.CommandType = CommandType.StoredProcedure;//声明存储过程
            try//异常处理机制
            {
                conn.Open();//打开数据库
                DataTable dt = new DataTable();
                SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.Fill(dt);//填充数据
                List<Feeder_tb> list = JsonConvert.DeserializeObject<List<Feeder_tb>>(JsonConvert.SerializeObject(dt));
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();//关闭数据库
                conn.Dispose();//释放内存
            }
        #endregion
        }
    }
}
